﻿<EditDataFormTemplateSpec 
	xmlns="bb_appfx_editdataformtemplate"
	xmlns:common="bb_appfx_commontypes" 
	ID="009036a6-bb25-4ca2-8ce9-ae90f4fb1b2d" 
	Name="Constituency Custom ID Edit Data Form"
	Description="Provides a mechanism for setting or editing an Constituency's custom ID" 
	Author="Blackbaud Professional Services"
	DataFormInstanceID="e87bede4-0389-4fcf-b198-1f1bb4a23120" 
	RecordType="Constituency"
	common:SecurityUIFolder="Service Bus Adapter"
	>

    <SPDataForm>

      <!-- define the edit load SP, which is used to load the data for the form -->
      <LoadImplementation SPName="USR_USP_DATAFORMTEMPLATE_EDITLOAD_CONSTITUENCYCUSTOMID">
        <common:CreateProcedureSQL>
          <![CDATA[
create procedure dbo.USR_USP_DATAFORMTEMPLATE_EDITLOAD_CONSTITUENCYCUSTOMID(
  @ID uniqueidentifier,
  @DATALOADED bit = 0 output,
  @TSLONG bigint = 0 output,
  @CUSTOMID nvarchar(255) = null output
)
as

	set nocount on;
  
  declare @CHANGEAGENTID uniqueidentifier;
  declare @CURRENTDATE datetime = getdate();
  exec dbo.USP_CHANGEAGENT_GETORCREATECHANGEAGENT @CHANGEAGENTID output
  
	-- be sure to set these, in case the select returns no rows
	set @DATALOADED = 0
	set @TSLONG = 0

	-- populate the output parameters, which correspond to fields on the form.  Note that
	-- we set @DATALOADED = 1 to indicate that the load was successful.  Otherwise, the system
	-- will display a "no data loaded" message.  Also note that we fetch the TSLONG so that concurrency
	-- can be considered.
  
  if exists (select 1 from dbo.USR_CONSTITUENCY where ID = @ID)
	  select
		  @DATALOADED = 1,
		  @TSLONG = TSLONG,
		  @CUSTOMID = LOOKUPID	
	  from 
      dbo.USR_CONSTITUENCY
	  where 
      ID = @ID
  else
    select
		  @DATALOADED = 1,
		  @CUSTOMID = ''

	return 0;
				]]>
        </common:CreateProcedureSQL>
      </LoadImplementation>

      <SaveImplementation SPName="USR_USP_DATAFORMTEMPLATE_EDIT_CONSTITUENCYCUSTOMID">
        <common:CreateProcedureSQL>
          <![CDATA[
create procedure dbo.USR_USP_DATAFORMTEMPLATE_EDIT_CONSTITUENCYCUSTOMID (
	@ID uniqueidentifier,
	@CHANGEAGENTID uniqueidentifier = null,
	@CUSTOMID nvarchar(255)
)
as

	set nocount on;

	if @CHANGEAGENTID is null  
		exec dbo.USP_CHANGEAGENT_GETORCREATECHANGEAGENT @CHANGEAGENTID output
				
	declare @CURRENTDATE datetime
	set @CURRENTDATE = getdate()

	begin try
		-- handle updating the data
    if exists (select 1 from dbo.USR_CONSTITUENCY where ID = @ID)
      begin
        update 
          dbo.USR_CONSTITUENCY
        set
			    CUSTOMIDENTIFIER = @CUSTOMID,
			    CHANGEDBYID = @CHANGEAGENTID,
			    DATECHANGED = @CURRENTDATE
		    where 
          ID = @ID
      end
    else
      begin
        insert into 
          dbo.USR_CONSTITUENCY 
            (ID, CUSTOMIDENTIFIER, DATEADDED, DATECHANGED, ADDEDBYID, CHANGEDBYID) 
        values 
          (@ID, @CUSTOMID, @CURRENTDATE, @CURRENTDATE, @CHANGEAGENTID, @CHANGEAGENTID)
      end
	end try
	begin catch
		exec dbo.USP_RAISE_ERROR
		return 1
	end catch

return 0;
				]]>
        </common:CreateProcedureSQL>
      </SaveImplementation>

    </SPDataForm>

    <!-- describe fields on the form, which correspond to parameters on the SP.  Note that system paramters 
	like the record ID, DATALOADED, TSLONG, and CURRENTAPPUSERID need not be listed. -->
    <common:FormMetaData FixedDialog="true">
      <common:FormFields>
        <common:FormField FieldID="CUSTOMID" Caption="Custom identifier" DataType="String" MaxLength="255" />
      </common:FormFields>

      <common:WebUIComponent>
        <common:WebUI>
          <common:DefaultWebUI/>
        </common:WebUI>
      </common:WebUIComponent>

    </common:FormMetaData>

  </EditDataFormTemplateSpec>